use super::{MessageButtons, MessageDialogResult, MessageLevel};
pub use rfd::{FileDialog, MessageDialog};
use std::path::PathBuf;
/// # 异步打开目录folder
/// ```rust
///  e_utils::dialog::sync::file()
/// ```
pub fn folder() -> Option<PathBuf> {
  FileDialog::new().pick_folder()
}
/// # 异步打开文件
/// ```rust
///  e_utils::dialog::sync::file()
/// ```
pub fn file() -> Option<PathBuf> {
  FileDialog::new().pick_file()
}
/// # 异步打开多个文件
/// ```rust
///  e_utils::dialog::sync::files()
/// ```
pub fn files() -> Option<Vec<PathBuf>> {
  FileDialog::new().pick_files()
}
/// # 异步打开多个目录
/// ```rust
///  e_utils::dialog::sync::folders()
/// ```
pub fn folders() -> Option<Vec<PathBuf>> {
  FileDialog::new().pick_folders()
}
/// # 异步创建文件
/// ```rust
///  e_utils::dialog::sync::create_file("newfile.txt")
/// ```
pub fn create_file(filename: impl Into<String>) -> Option<PathBuf> {
  FileDialog::new().set_file_name(filename).save_file()
}
/// # Ok
/// ```rust
///  e_utils::dialog::sync::ok("Title","test")
/// ```
pub fn ok(title: &str, msg: impl Into<String>) {
  custom_one(title, msg, "OK")
}
/// # 确认
/// ```rust
///  e_utils::dialog::sync::ok_zh("Title","test")
/// ```
pub fn ok_zh(title: &str, msg: impl Into<String>) {
  custom_one(title, msg, "确认")
}
/// # Yes No
/// ```rust
///  e_utils::dialog::sync::yesno_zh("Title","test")
/// ```
pub fn yesno_zh(title: &str, msg: impl Into<String>) -> bool {
  custom_tow(title, msg, "同意", "拒绝")
}
/// # Yes NO
/// ```rust
///  e_utils::dialog::sync::yesno("Title","test")
/// ```
pub fn yesno(title: &str, msg: impl Into<String>) -> bool {
  custom_tow(title, msg, "YES", "NO")
}
/// # Info
/// ```rust
///  e_utils::dialog::sync::info("Title","test")
/// ```
pub fn info(title: &str, msg: impl Into<String>) {
  show(title, msg, MessageLevel::Info).show();
}
/// # Error
/// ```rust
///  e_utils::dialog::sync::error("Title","test")
/// ```
pub fn error(title: &str, msg: impl Into<String>) {
  show(title, msg, MessageLevel::Error).show();
}
/// # Warn
/// ```rust
///  e_utils::dialog::sync::warn("Title","test")
/// ```
pub fn warn(title: &str, msg: impl Into<String>) {
  show(title, msg, MessageLevel::Warning).show();
}
#[inline]
fn custom_tow(
  title: &str,
  msg: impl Into<String>,
  custom1: impl Into<String>,
  custom2: impl Into<String>,
) -> bool {
  let dialog = show(title, msg, MessageLevel::Info)
    .set_buttons(MessageButtons::OkCancelCustom(
      custom1.into(),
      custom2.into(),
    ))
    .show();
  if let MessageDialogResult::Ok = dialog {
    true
  } else {
    false
  }
}
#[inline]
fn custom_one(title: &str, msg: impl Into<String>, custom: impl Into<String>) {
  show(title, msg, MessageLevel::Info)
    .set_buttons(MessageButtons::OkCustom(custom.into()))
    .show();
}
#[inline]
fn show(title: &str, msg: impl Into<String>, msg_type: MessageLevel) -> MessageDialog {
  MessageDialog::new()
    .set_title(title)
    .set_description(msg)
    .set_level(msg_type)
}

#[cfg(test)]
mod test {
  use crate::dialog::sync::*;
  #[test]
  fn test_custom_tow() {
    assert_eq!(custom_tow("测试", "YES NO", "同意", "拒绝"), true)
  }
  #[test]
  fn test_custom_one() {
    assert_eq!(custom_one("测试", "custom_one", "确认"), ())
  }
  #[test]
  fn test_show() {
    assert_eq!(
      {
        show("测试", "show", MessageLevel::Info).show();
      },
      ()
    )
  }
  #[test]
  fn test_folder() {
    assert!(folder().is_some())
  }

  #[test]
  fn test_folders() {
    assert!(folders().is_some())
  }

  #[test]
  fn test_files() {
    assert!(files().is_some())
  }
  #[test]
  fn test_file() {
    assert!(file().is_some())
  }
  #[test]
  fn test_create_file() {
    assert!(create_file("test.txt").is_some())
  }
}
